{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0252a526-fc40-4b78-8cb1-bf54d3a57e61",
   "metadata": {},
   "source": [
    "## 缺省函数\n",
    "- 定义函数时，"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "a7b60fbb-e044-404c-bba4-7cc40e8bc1a2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3, 6, 9]\n",
      "[9, 6, 3]\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "\n",
    "gl_num_list = [6, 3, 9]\n",
    "\n",
    "#默认值是升序排序，因为这种应用需求更多\n",
    "gl_num_list.sort()\n",
    "print(gl_num_list)\n",
    "\n",
    "#只有当需要降序排序时，才需要传递\n",
    "gl_num_list.sort(reverse=True)\n",
    "print(gl_num_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "f884021e-2ff5-4a23-8dcf-fe2efbdb6f5d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "莉莉 是 女生\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "def print_info(name, gender=True):\n",
    "    gender_text = '女生'\n",
    "    if not gender:\n",
    "        gender_text = '男生'\n",
    "\n",
    "    print('%s 是 %s' % (name, gender_text))\n",
    "\n",
    "print_info('莉莉')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fb712e21-8ce3-48e2-a183-c433db3fee07",
   "metadata": {},
   "source": [
    "#### 元组和字典的拆包\n",
    "- 在调用有多个参数的函数时，如果希望\n",
    "      - 将一个元组变量，直接传递给参数\n",
    "      - 将一个字典遍历，直接传递给参数\n",
    "- 就可以使用拆包，简化参数的传递，拆包的方式是\n",
    "      - 在元组变量前，增加一个 *\n",
    "      - 在字典遍历前，增加两个 **"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2ab102ef-bba9-4030-939b-f5b1c73dd845",
   "metadata": {},
   "outputs": [],
   "source": [
    "# coding:UTF-8\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f3e8bbab-813e-4efe-9eab-4c54a49d12af",
   "metadata": {},
   "source": [
    "## 函数的递归"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "17020afe-da72-44df-9a64-d8fa4757117d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "2\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "# 递归就是函数内部自己调用自己操作\n",
    "# coding:UTF-8\n",
    "\n",
    "def sum_numsers(num):\n",
    "    print(num)\n",
    "\n",
    "    #递归调用出口很重要，否则就会造成死循环\n",
    "    if num == 1:\n",
    "        return\n",
    "\n",
    "    sum_numsers(num - 1)    #递归调用，自己调用自己\n",
    "\n",
    "sum_numsers(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "4a58216b-3135-4841-8513-84a4c2ae3f08",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "#利用递归进行数字的累加\n",
    "# coding:UTF-8\n",
    "def sum_numbers(num):\n",
    "\n",
    "    if num == 1:\n",
    "        return 1\n",
    "\n",
    "    #假设 sun_numbers能够完成 num - 1 的累加\n",
    "    temp = sum_numbers(num - 1)\n",
    "\n",
    "    #函数内部的核心算法就是两个数字相加\n",
    "    return num + temp \n",
    "\n",
    "print(sum_numbers(2))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ee9c0ec9-f865-45e9-8e67-945f6cc9b8f1",
   "metadata": {},
   "source": [
    "## 集合"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e9e47374-6705-4c60-ba35-42bd0057314e",
   "metadata": {},
   "source": [
    "## 集合的常见操作\n",
    "##### 增加数据\n",
    "##### add()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "aa8a3ace-e03e-4b75-a80c-2395e8c85745",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{100, 10, 20}\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "s1 = {10, 20}\n",
    "s1.add(100)\n",
    "s1.add(10)    # 集合不支持重复数据\n",
    "print(s1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "980a1f11-30b9-4c62-ac87-3407dca9c587",
   "metadata": {},
   "source": [
    "因为集合有去重功能，所以，当前集合内追加的数据是当前集合已有的数据，则不进行任何操作\n",
    "update() 追加的数据是序列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "cec652c3-48b1-490e-82ea-aa6c53e5f78a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{100, 200, 10, 'a', 20, 'b', 'c'}\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "\n",
    "s1 = {10, 20}\n",
    "s1.update([100, 200])\n",
    "s1.update('abc')\n",
    "print(s1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "559889b7-7ac4-4de1-9a9d-f10f21be944b",
   "metadata": {},
   "source": [
    "#### 删除数据\n",
    "remove() 删除集合中的指定数据，如果数据不存在则报错"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "ca6f80eb-95ba-4b87-935c-7f5471925f19",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{20}\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "s1 = {10, 20}\n",
    "\n",
    "s1.remove(10)\n",
    "print(s1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "46f59f89-912b-4ff9-a9ca-1e1d2a0f8082",
   "metadata": {},
   "source": [
    "discard() 删除集合中的固定数据，如果数据不存在也不会报错"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "0dd3d611-3940-4880-945c-b7ce3a6c2382",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{20}\n",
      "{20}\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "s1 = {10, 20}\n",
    "\n",
    "s1.discard(10)\n",
    "print(s1)\n",
    "\n",
    "s1.discard(10)\n",
    "print(s1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e4b4d22c-4a97-4ee9-837d-2b3e94cf47c9",
   "metadata": {},
   "source": [
    "pop() 随即删除集合中的某个数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "52143dd6-36ed-4fc6-981b-ae60e45173a9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "50\n",
      "{20, 40, 10, 30}\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "\n",
    "s1 = {10, 20, 30, 40, 50}\n",
    "\n",
    "del_num = s1.pop()\n",
    "print(del_num)\n",
    "print(s1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d2c2c365-db75-4b49-87ab-1e54e5e5fccd",
   "metadata": {},
   "source": [
    "#### 查找数据\n",
    "in 判断数据在集合序列中\n",
    "not in 判断数据不在集合序列中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "f7dd87be-f992-42b5-98d7-a0d3cb6fcdcc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "\n",
    "s1 = {10, 20, 30, 40, 50}\n",
    "\n",
    "print(10 in s1)\n",
    "print(10 not in s1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8ae30363-1e87-45a5-a370-abcd16bc4146",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 列表推导式\n",
    "用一个表达式创建一个源头规律的列表或控制一个有规律的列表\n",
    "创建一个0~10的列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "397da0f2-328e-42a3-a806-2510c9fc7295",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "\n",
    "# 准备一个空列表\n",
    "list1 = []\n",
    "\n",
    "#编写循环，依次追加数字到空列表list1中\n",
    "i = 0\n",
    "while i < 10:\n",
    "    list1.append(i)\n",
    "    i += 1\n",
    "\n",
    "print(list1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "9d6e13e7-d39c-417b-8748-ac070158ac43",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "\n",
    "# 准备一个空列表\n",
    "list1 = []\n",
    "\n",
    "for i in range(10):\n",
    "    list1.append(i)\n",
    "\n",
    "print(list1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fff32a86-e312-4229-94da-52ffa0cb5804",
   "metadata": {},
   "outputs": [],
   "source": [
    "- 列表推导式实现\n",
    "  创建0~10的偶数列表\n",
    "  range() 步长实现"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "8c2927e2-21de-49e9-b2c8-48048a4d00ac",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 2, 4, 6, 8]\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "\n",
    "list1 = [i for i in range(0, 10, 2)]\n",
    "print(list1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "713dcdc6-0e16-49e3-9d67-3633f8ebad8b",
   "metadata": {},
   "source": [
    "if 实现"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "be57c888-9771-43de-b2b9-7b5abbefb213",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "49ae014a-85f2-46ff-b4c4-e841d3f37992",
   "metadata": {},
   "source": [
    "多个 for 循环实现列表推导式\n",
    "要求：\n",
    "[(1,0), (1,1), (1,2), (2,0), (2,1), (2,2)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "71dc1aa3-9937-4f5a-816c-8e37cb27847a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "\n",
    "list1 = [(i, j) for i in range (1, 3) for j in range(3)]\n",
    "print(list1) "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bff48c1f-b773-4a73-a5dc-48ee3e2b5f33",
   "metadata": {},
   "source": [
    "## 字典推导式"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6ea400ca-3392-4e70-bd64-13405f5eb5f1",
   "metadata": {},
   "source": [
    "list1 = ['name', 'age', 'gender']\n",
    "list2 = ['张三', '22', '男']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "618a5b33-44f6-4d53-8f75-dccac5c09be0",
   "metadata": {},
   "source": [
    "将以上两个列表快速合并为一个字典\n",
    "这时就要用到字典推导式\n",
    "创建一个字典，字典key是数字1~5, value是这个数字的2次方"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "cb754c5d-d019-4ed2-9db7-b97b2bca892b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1: 1, 2: 4, 3: 9, 4: 16}\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "\n",
    "dict1 = {i: i**2 for i in range(1, 5)}    #字典推导式 一行解决之前好几行代码的功能\n",
    "print(dict1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2957cdab-5603-4515-9305-55934850fb59",
   "metadata": {},
   "source": [
    "将两个列表合并为一个字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d475b326-9136-4d5c-ab59-9440eb4974d1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': '张三', 'age': '22', 'gender': '男'}\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "\n",
    "list1 = ['name', 'age', 'gender']\n",
    "list2 = ['张三', '22', '男'] \n",
    "\n",
    "dict1 = {list1[i]: list2[i] for i in range(len(list1))}\n",
    "print(dict1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f8e4ed52-3763-45c7-ba96-b8bfb6972d23",
   "metadata": {},
   "source": [
    "提取字典中目标数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "a7852d9d-2bb8-4b6e-8b81-ad08a32accd6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'MAP': 268, 'DELL': 201}\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "\n",
    "content = {'MAP': 268, 'UAV': 125, 'DELL': 201, 'ACER': 199}\n",
    "\n",
    "#需求：提取上述电脑数量大于等于200的字典数据\n",
    "count1 = {key: value for key, value in content.items() if value >= 200}\n",
    "print(count1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fdac9424-cdea-444b-9e5c-79d13ebfac76",
   "metadata": {},
   "source": [
    "## 集合推导式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "57a28fee-d7fa-4214-be56-912f629d9cab",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 4}\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "\n",
    "sss1 = [1, 1, 2]\n",
    "set1 = {i ** 2 for i in sss1}\n",
    "print(set1)    # 1的二次方还是1，由于集合1不想字典那样，他不能存储重复的数据，所以这里的1只能保留1个"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6da9cb70-505a-4a61-9b22-4e1df1f79475",
   "metadata": {},
   "source": [
    "推导式的作用及还有，就是简化代码"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9036336a-a3e0-4e99-8eb2-63c572193103",
   "metadata": {},
   "source": [
    "## 三个推导式的公式\n",
    "- 列表推导式：\n",
    "- [xx for xx in range(起始范围要求, 终止范围要求不包括最后一位, 步长)]\n",
    "\n",
    "- 字典推导式：\n",
    "- [xx1；xx2 for ... in ...]\n",
    "\n",
    "- 集合推导式：\n",
    "- {xx for xx in ...}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "07a8b040-4740-4dec-b74e-6675c14eed69",
   "metadata": {},
   "source": [
    "- lambda() 函数\n",
    "- 用于创建匿名函数，匿名函数就是没有名称的函数，也不再使用def 语句定义的函数，语法格式：\n",
    "- lambda [arg1 [,arg2,.....argsn]]:expression\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "85602dec-670a-4a7d-952b-53e23b70546c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "\n",
    "sum = lambda a, b: a + b    #定义一个匿名函数，没有名字，也没有def\n",
    "#相当于\n",
    "#def sum(a, b):\n",
    "#    return a + b\n",
    "\n",
    "print(sum(1, 2))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "58876d20-896a-4e6b-a202-5270c6ea202b",
   "metadata": {},
   "source": [
    "以上代码中创建了一个两数求和的匿名函数，为了保证不被内存回收，最好赋值给一个变量，而后变量可以像函数那样调用"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e25e1dc6-79f0-436d-a647-53b4730c5284",
   "metadata": {},
   "source": [
    "map() 函数定义\n",
    "会根据提供的函数对指定的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "e71f1f54-245f-45ba-adcb-6bb883d4b68c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3, 4, 5, 6, 7]\n",
      "[5, 7, 9]\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "\n",
    "func = lambda x : x+2\n",
    "#相当于\n",
    "#def func(x):\n",
    "#    return x+2\n",
    "\n",
    "result = map(func, [1,2,3,4,5])    #使用map()函数，并将调用的函数名，作为参数传递给map函数\n",
    "print(list(result))    #将内容转换成列表\n",
    "\n",
    "result = map(lambda x, y: x+y, [1,2,3],[4,5,6])\n",
    "#相当于\n",
    "#def result(x, y)\n",
    "#    return x + y\n",
    "print(list(result))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "71f2bfa0-e5d1-487c-a150-551a8d382f86",
   "metadata": {},
   "source": [
    "列表推导式、字典推导式、集合推导式，以及lambda匿名函数都是建立在之前基础上的。如果之前基础不扎实，将无法无法理解这些操作"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
